<template>
  <div>
    <!-- 搜索栏 -->
    <el-card id="search">
      <el-row>
        <el-col :span="20">
          <el-input
            v-model="searchModel.name"
            placeholder="岗位名称"
            clearable
          ></el-input>
          <el-button
            @click="getPostList"
            type="primary"
            round
            icon="el-icon-search"
          >查询</el-button>
        </el-col>
        <el-col :span="4" align="right">
          <el-button
            @click="openEditUI(null)"
            type="primary"
            icon="el-icon-plus"
            circle
          ></el-button>
        </el-col>
      </el-row>
    </el-card>
    <el-card>
      <el-table :data="postList" stripe style="width: 100%">
        <el-table-column label="#" width="80">
          <template slot-scope="scope">
            <!-- (pageNo-1) * pageSize + index + 1 -->
            {{
              (searchModel.pageNo - 1) * searchModel.pageSize + scope.$index + 1
            }}
          </template>
        </el-table-column>
        <el-table-column prop="postNo" label="岗位编号" width="180">
        </el-table-column>
        <el-table-column prop="name" label="岗位名称" width="180">
        </el-table-column>
        <el-table-column prop="teacher" label="负责老师"> </el-table-column>
        <el-table-column>
        <template slot-scope="scope">
            <el-button @click="openEditUI(scope.row.id)" type="primary" icon="el-icon-edit" size="mini" circle></el-button>
            <el-button @click="deletePost(scope.row)" type="danger" icon="el-icon-delete" size="mini" circle></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
<!-- 分页组件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="searchModel.pageNo"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="searchModel.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
<!-- 用户信息编辑对话框 -->
<el-dialog
  @close="clearForm"
  :title="title"
  :visible.sync="dialogFormVisible"
>
<el-form :model="postForm" ref="postFormRef" :rules="rules">
  <el-form-item
    label="岗位编号"
    prop="postNo"
    :label-width="formLabelWidth"
  >
    <el-input v-model="postForm.postNo" autocomplete="off"></el-input>
  </el-form-item>
  <el-form-item
    label="岗位名称"
    prop="name"
    :label-width="formLabelWidth">
    <el-input v-model="postForm.name" autocomplete="off"></el-input>
  </el-form-item>
  <el-form-item
    label="关联学生"
    prop="students"
    :label-width="formLabelWidth"
  >
    <el-select
      v-model="postForm.students"
      multiple
      filterable
      remote
      reserve-keyword
      placeholder="请输入关键词"
      :remote-method="remoteMethod"
      :loading="loading">
      <el-option
        v-for="item in studentList"
        :key="item.id"
        :label="item.name"
        :value="item.id">
      </el-option>
    </el-select>
  </el-form-item>
  </el-form>
<div slot="footer" class="dialog-footer">
  <el-button @click="dialogFormVisible = false">取 消</el-button>
  <el-button type="primary" @click="savePost">确 定</el-button>
</div>
</el-dialog>
</div>
</template>

<script>
import postApi from "@/api/post";
import userApi from "@/api/userManage";
import studentApi from "@/api/student";
export default {
  data() {
    return {
      title: "",
      total: 0,
      formLabelWidth: "130px",
      searchModel: {
        pageNo: 1,
        pageSize: 10
      },
      teacherList: [],
      studentList: [],
      postList: [],
      postForm: {},
      rules: {
        postNo: [
          {required: true, message: "请输入岗位编号", trigger: "blur"},
          {
            min: 3,
            max: 50,
            message: "长度在 3 到 50 个字符",
            trigger: "blur",
          },
        ],
        name: [
          {required: true, message: "请输入岗位名称", trigger: "blur"},
          {
            min: 3,
            max: 16,
            message: "长度在 3 到 16 个字符",
            trigger: "blur",
          },
        ],
        teacher: [
          {required: true, message: "请输入负责教师", trigger: "blur"},
          {
            min: 2,
            max: 20,
            message: "长度在 2 到 20 个字符",
            trigger: "blur",
          },
        ]
      },
      dialogFormVisible: false,
    }
  },
  methods: {
    handleSizeChange(pageSize) {
      this.searchModel.pageSize = pageSize;
      this.getPostList();
    },
    handleCurrentChange(pageNo) {
      this.searchModel.pageNo = pageNo;
      this.getPostList();
    },
    getPostList() {
      postApi.getPostList(this.searchModel).then((response) => {
        this.postList = response.data.rows
        this.total = response.data.total
      })
    },
    getStudentList() {
      studentApi.all().then((response) => {
        this.studentList = response.data
      })
    },
    deletePost(post) {
      this.$confirm(`您确认删除岗位 ${post.name} ?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        postApi.deletePostById(post.id).then(response => {
          this.$message({
            type: 'success',
            message: response.message
          })
          this.getPostList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    savePost() {
      // 触发表单验证
      this.$refs.postFormRef.validate((valid) => {
        if (valid) {
          // 提交请求给后台
          postApi.savePost(this.postForm).then(response => {
            // 成功提示
            this.$message({
              message: response.message,
              type: 'success'
            })
            // 关闭对话框
            this.dialogFormVisible = false
            // 刷新表格
            this.getPostList()
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    clearForm() {
      this.postForm = {}
      this.$refs.postFormRef.clearValidate()
    },
    openEditUI(id) {
      if (id == null) {
        this.title = '添加岗位'
      } else {
        this.title = '修改岗位'
        // 根据id查询用户数据
        postApi.getPostById(id).then(response => {
          this.postForm = response.data
        })
      }
      this.dialogFormVisible = true
    }
  },
  created() {
    this.getPostList()
    this.getStudentList()
  }
}
</script>

<style>

</style>
